package com.wewins.fota.sys.service;

import com.wewins.fota.bean.po.UserInfo;
import com.wewins.fota.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service("userDetailsService")
@Slf4j
public class TokenUserDetailsService implements UserDetailsService {
    private final UserInfoService userInfoService;

    @Autowired
    public TokenUserDetailsService(UserInfoService userInfoService) {
        this.userInfoService = userInfoService;
    }

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        UserInfo user;
        try {
            return userInfoService.getUserInfoByUsername(userName);
        } catch (Exception e) {
            log.error("user select fail", e);
            throw new UsernameNotFoundException("user select fail", e);
        }
    }
}
